package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.MesProduceOrder;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.MesProduceOrderDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2022-03-30 15:28:51
 */
public class MesProduceOrderConverts {

    private static Logger LOG = LoggerFactory.getLogger(MesProduceOrderConverts.class);

    public static List<MesProduceOrderDTO> convert(List<MesProduceOrder> mesProduceOrderList) {
        if (CollectionUtils.isEmpty(mesProduceOrderList)) {
            return Collections.EMPTY_LIST;
        }

        List<MesProduceOrderDTO> mesProduceOrderListResult = Lists.newArrayList();
        for (MesProduceOrder mesProduceOrder : mesProduceOrderList) {
            mesProduceOrderListResult.add(convert(mesProduceOrder));
        }
        return mesProduceOrderListResult;
    }

    public static MesProduceOrderDTO convert(MesProduceOrder mesProduceOrder) {
        try {
            return BeanUtils.copy(mesProduceOrder, MesProduceOrderDTO.class);
        } catch (Exception e) {
            LOG.error("MesProduceOrder到MesProduceOrderDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "MesProduceOrder到MesProduceOrderDTO转换失败", e);
        }
    }

    public static MesProduceOrder convert(MesProduceOrderDTO mesProduceOrderDTO) {
        try {
            return BeanUtils.copy(mesProduceOrderDTO, MesProduceOrder.class);
        } catch (Exception e) {
            LOG.error("MesProduceOrderDTO到MesProduceOrder转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "MesProduceOrderDTO到MesProduceOrder转换失败", e);
        }
    }
}